package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealOverViewVO;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@Api(tags = "套餐管理")
@RequestMapping("/admin/setmeal")
public class SetmealController {
    private final SetmealService setmealService;

    @Autowired
    public SetmealController(SetmealService setmealService){
        this.setmealService = setmealService;
    }

    @PostMapping
    @ApiOperation("增加套餐")
    @CacheEvict(cacheNames = "setmealCache", key = "#setmealDTO.categoryId")
    public Result<String> add(@RequestBody SetmealDTO setmealDTO){
        log.info("新增套餐 {}", setmealDTO);
        this.setmealService.add(setmealDTO);
        return Result.success();
    }

    @GetMapping("/{id}")
    @ApiOperation("查询套餐")
    public Result<SetmealVO> getById(@PathVariable Long id){
        log.info("查询套餐 {}", id);
        SetmealVO setmealVO = this.setmealService.getById(id);
        return Result.success(setmealVO);
    }

    @GetMapping("/page")
    @ApiOperation("分页查询")
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO){
        log.info("分页查询套餐 {}", setmealPageQueryDTO);
        PageResult pageResult = this.setmealService.page(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    @PostMapping("/status/{status}")
    @ApiOperation("设置状态")
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result<String> setStatus(@PathVariable int status, Long id){
        log.info("设置套餐 {} 状态为 {}", id, status);
        this.setmealService.setStatus(status, id);
        return Result.success();
    }

    @PutMapping
    @ApiOperation("修改套餐")
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result<String> update(@RequestBody SetmealDTO setmealDTO){
        log.info("修改套餐 {}", setmealDTO);
        this.setmealService.update(setmealDTO);
        return Result.success();
    }

    @DeleteMapping
    @ApiOperation("批量删除套餐")
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result<String> delete(@RequestParam List<Long> ids){
        log.info("删除的菜品有 {}", ids);
        this.setmealService.delete(ids);
        return Result.success();
    }

}
